package com.testmanager.testmananger.pagetest;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.testmanager.testmananger.user.domain.QUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;


@Repository
@Transactional(readOnly = true)
public class BookRepositoryCustom {
    @Autowired
    BookRepository bookRepository;

   /* private Integer id;
    private String title;
    private  Integer type;
    private double price;
*/
    public Page<BookEntity> search(BookEntity bookEntity, int numble,int size){
        QBookEntity qBookEntity = QBookEntity.bookEntity;
        BooleanExpression expression = qBookEntity.id.isNotNull();
        if (bookEntity.getPrice()!= 0){
            expression = expression.and(qBookEntity.price.eq(qBookEntity.price));
        }
        if (StringUtils.isNotEmpty(bookEntity.getTitle())){
            expression = expression.and(qBookEntity.title.like("%"+bookEntity.getTitle()+"%"));
        }
        Sort orders = Sort.by(Sort.Order.desc(qBookEntity.id.getMetadata().getName()),Sort.Order.desc(qBookEntity.price.getMetadata().getName()));
        Pageable pageableSort = PageRequest.of(numble, size,orders);
        return bookRepository.findAll(expression,pageableSort);
    }


    /*public Page exchange(Page<BookEntity> page){

               page.nextPageable().
    }*/
}
